<?php
$this->load->model("core/media");
class ModelAddonOrder extends ModelCoreMedia
{ 
	public function getItem($orderid, $where="")
	{
		$orderid=$this->db->escape(@$orderid);
		$query = $this->db->query("Select `order`.* 
									from `order` 
									where orderid ='".$orderid."' ".$where);
		$data['order'] = $query->row;
		$sql = "Select `order_product`.* , media.title , media.unit,media.refersitemap
									from `order_product` ,media
									where orderid = '".$orderid."' And order_product.mediaid = media.mediaid Order by id";
		$query = $this->db->query($sql);
		$data['detail']=$query->rows;
		$data['sum'] = 0;
		foreach($data['detail'] as $item)
		{
			$total = $item['subtotal'];
			$data['sum']+=$total;
		}
		$data['sum'] = $this->string->numberParse($data['sum']);
		//$data['sum'] = $this->string->numberParse($data['order']['total']);
		return $data;
	}
	
	public function getListOrder($where="", $from=0, $to=0)
	{
		
		$sql = "Select `order`.* 
									from `order` 
									where 1=1 " . $where;
		if($to > 0)
		{
			$sql .= " Limit ".$from.",".$to;
		}
		$query = $this->db->query($sql);
		return $query->rows;
	}

	public function insert($data)
	{
		$type=$this->db->escape(@$data['type']);
		$orderid= $type.time();
		$createby=$this->db->escape(@$data['createby']);
		$supplier=$this->db->escape(@$data['supplier']);
		$orderdate=$this->db->escape(@$data['orderdate']);
		$userid = $this->user->getId();
		
		$customername=$this->db->escape(@$data['customername']);
		$address=$this->db->escape(@$data['address']);
		$email=$this->db->escape(@$data['email']);
		$phone=$this->db->escape(@$data['phone']);
		$status=$this->db->escape(@$data['status']);
		$comment=$this->db->escape(@$data['comment']);
		
		
		
		$field=array(
						'orderid',
						'createby',
						'supplier',
						'orderdate',
						'userid',
						
						'customername',
						'address',
						'email',
						'phone',
						'status',
						'comment',
						'type'
						
					);
		$value=array(
						$orderid,
						$createby,
						$supplier,
						$orderdate,
						$userid,
						
						$customername,
						$address,
						$email,
						$phone,
						$status,
						$comment,
						$type
						
					);
		$this->db->insertData("order",$field,$value);
		return $orderid;
	}
	
	public function update($data)
	{
		$orderid =$this->db->escape( @$data['orderid']);
		$createby=$this->db->escape(@$data['createby']);
		$supplier=$this->db->escape(@$data['supplier']);
		$orderdate=$this->db->escape(@$data['orderdate']);
		$userid = $this->user->getId();
		
		$customername=$this->db->escape(@$data['customername']);
		$address=$this->db->escape(@$data['address']);
		$email=$this->db->escape(@$data['email']);
		$phone=$this->db->escape(@$data['phone']);
		$status=$this->db->escape(@$data['status']);
		$comment=$this->db->escape(@$data['comment']);
		
		$field=array(
						'orderdate',
						'createby',
						'supplier',
						'customername',
						'userid',
						
						'address',
						'email',
						'phone',
						'status',
						'comment',
						
					);
		$value=array(
						$orderdate,
						$createby,
						$supplier,
						$customername,
						$userid,
						
						$address,
						$email,
						$phone,
						$status,
						$comment,
						
					);
		
		$where="orderid = '".$orderid."'";
		$this->db->updateData('order',$field,$value,$where);
		return $orderid;
	}
	
	public function updateTotal($orderid)
	{
		$orderid =$this->db->escape( @$orderid);
		$sql = "Select `order_product`.* , media.title , media.unit,media.refersitemap
									from `order_product` ,media
									where orderid = '".$orderid."' And order_product.mediaid = media.mediaid Order by id";
		$query = $this->db->query($sql);
		$data['detail']=$query->rows;
		$data['sum'] = 0;
		foreach($data['detail'] as $item)
		{
			$total = $item['subtotal']*(1 - $item['discount']/100);
			$data['sum']+=$total;
		}
		$total = $this->string->numberParse($data['sum']);;
		$field=array(
						'total'
					);
		$value=array(
						$total
						
					);
		
		$where="orderid = '".$orderid."'";
		$this->db->updateData('order',$field,$value,$where);
		return $orderid;
	}
	
	public function delete($orderid)
	{
		$orderid=$this->db->escape(@$orderid);
		$where="orderid = '".$orderid."'";
		$order = $this->getItem($orderid);
		$this->db->deleteData('order_product',$where);
		$this->db->deleteData('order_information',$where);
		$this->db->deleteData('order',$where);
		
		/*//Cap nhat lai ton kho
		foreach($order["detail"] as $item)
		{
			$datamedia['mediaid'] = $item['mediaid'];
			$datamedia['quantity'] = $this->getInstock($item['mediaid']);
			$this->updateQuantity($datamedia);	
		}*/
		$this->updataInstock();	
	}
	
	public function getTotalQuantity($mediaid,$listorderid)
	{
		
		$sql = "Select `order_product`.* , sum(quantity) as totalquantity 
									from `order_product` 
									where orderid in ('".implode("','",$listorderid)."') AND mediaid = '".$mediaid."'
									GROUP BY mediaid
									";
		$query = $this->db->query($sql);
		return $query->row['totalquantity'];
	}
	
	//order product
	public function getListOrderProduct($where)
	{
		$sql = "Select `order_product`.*  
									from `order_product` 
									where 1=1 ".$where."		
									";
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function getOrderProduct($orderid,$mediaid)
	{
		$orderid=$this->db->escape(@$orderid);
		$mediaid=$this->db->escape(@$mediaid);
		$sql = "Select `order_product`.* 
									from `order_product` 
									where orderid = '".$orderid."' And mediaid='".$mediaid."'";
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
	public function saveOrderProduct($data)
	{
		
		$id=$this->db->escape(@$data['id']);
		$orderid=$this->db->escape(@$data['orderid']);
		$mediaid=$this->db->escape(@$data['mediaid']);
		$quantity=$this->db->escape(@$data['quantity']);
		$price=$this->db->escape(@$data['price']);
		$discount=$this->db->escape(@$data['discount']);
		$subtotal=$this->string->numberParse(@$data['subtotal']);
		$type=$this->db->escape(@$data['type']);
		$field=array(
						'orderid',
						'mediaid',
						'quantity',
						'price',
						'discount',
						'subtotal',
						'type'
					);
		$value=array(
						$orderid,
						$mediaid,
						$quantity,
						$price,
						$discount,
						$subtotal,
						$type
					);
		//$arr=$this->getOrderProduct($orderid,$mediaid);
		if($id=="")
		{
			$this->db->insertData("order_product",$field,$value);
		}
		else
		{
			$where="id = '".$id."'";
			$this->db->updateData('order_product',$field,$value,$where);
		}
		/*$datamedia['mediaid'] = $mediaid;
		$datamedia['quantity'] = $this->getInstock($mediaid);
		$this->updateQuantity($datamedia);*/
		$this->updataInstock();
	}
	
	function removeOrderProduct($id)
	{
		$id=$this->db->escape(@$id);
		$where="id = '".$id."'";
		$this->db->deleteData('order_product',$where);
		$this->updataInstock();
	}
	
	function getInforOrder($orderid,$fieldname)
	{
		$sql = "Select * 
					from `order_information` 
					where fieldname ='".$fieldname."' ";
		$query = $this->db->query($sql);
		return $query->row;
	}
	
	function getInforOrderValue($orderid,$fieldname)
	{
		$sql = "Select * 
					from `order_information` 
					where fieldname ='".$fieldname."' ";
		$query = $this->db->query($sql);
		return $query->row['fieldvalue'];
	}
	
	function saveInforOrder($orderid,$fieldname,$fieldvalue)
	{
		$orderid=$this->db->escape(@$orderid);
		$fieldname=$this->db->escape(@$fieldname);
		$fieldvalue=$this->db->escape(@$fieldvalue);
		$field=array(
						'orderid',
						'fieldname',
						'fieldvalue'
					);
		$value=array(
						$orderid,
						$fieldname,
						$fieldvalue
					);
		$arr = $this->getInforOrder($orderid,$fieldname);
		if(count($arr)==0)
			$this->db->insertData("order_information",$field,$value);
		else
		{
			$where="orderid = '".$orderid."'";
			$this->db->updateData('order_information',$field,$value,$where);	
		}
	}
}
?>